﻿using System;
using System.Collections.Generic;
using System.Linq;
using BoldBrick.OneIntranet.HR.DataObjects.EventReceiverObjects;
using BoldBrick.OneIntranet.HR.DataObjects.Lists.CandidatesListData.Views;
using BoldBrick.OneIntranet.HR.Resources;
using BoldBrick.SharePoint.CommonControls.DataObjects.Base;
using Microsoft.SharePoint;

namespace BoldBrick.OneIntranet.HR.DataObjects.Lists.CandidatesListData
{
	/// <summary>
	/// Metadata class of "Candidates" list
	/// </summary>
	public class CandidatesList : BaseList, IContentTypeUniqueOrderService
	{
		#region ------ Views ----------------------------------------------------------------------

		private CandidatesView candidatesView;
		public CandidatesView CandidatesView
		{
			get
			{
				if (candidatesView == null)
					candidatesView = new CandidatesView();
				return candidatesView;
			}
		}
		
		private CandidateView candidateView;
		public CandidateView CandidateView
		{
			get
			{
				if (candidateView == null)
					candidateView = new CandidateView();
				return candidateView;
			}
		}

		public override ViewsCollection GetViews()
		{
			ViewsCollection views = new ViewsCollection
			                        	{
			                        		this.CandidatesView, 
			                        		this.CandidateView
			                        	};
			return views;
		}

		#endregion

		#region ------ Workflows ------------------------------------------------------------------

		private Workflow hrWorkflow;
		public Workflow HrWorkflow
		{
			get
			{
				if (hrWorkflow == null)
				{
					hrWorkflow = new Workflow(new Guid("b4852b38-4c53-47e6-8f4f-30f51d2cb846"), 
						ResourceBuilder.ResourceFileName, "Workflow_HRWorkflow_Title",
						Lists.WorkflowHistoryList, Lists.TasksList)
					             	{
					             		ContentTypeID = ContentTypes.ContentTypes.CandidateContentType.ID
					             	};
				}
				return hrWorkflow;
			}
		}

		#endregion

		public CandidatesList()
		{
			this.EnableFolderCreation = false;
			
		}

		protected override string GetListTitleResourceKey()
		{
			return "List_Candidates_Title";
		}

		protected override string GetListNameResourceKey()
		{
			return "List_Candidates_Name";
		}

		protected override SPListTemplateType? GetListTemplateType()
		{
			return SPListTemplateType.GenericList;
		}

		public override List<BaseContentType> GetContentTypes()
		{
			List<BaseContentType> contentTypes = new List<BaseContentType>
			                                     	{
			                                     		ContentTypes.ContentTypes.CandidateContentType,
			                                     		ContentTypes.ContentTypes.InterviewResultContentType,
			                                     		ContentTypes.ContentTypes.ReferenceCallResultContentType,
			                                     		ContentTypes.ContentTypes.TestResultContentType,
			                                     		ContentTypes.ContentTypes.FinalDecisionResultContentType,
			                                     		ContentTypes.ContentTypes.CVDocumentContentType,
			                                     		ContentTypes.ContentTypes.ReviewResultContentType,
			                                     		ContentTypes.ContentTypes.CandidateDecisionResultContentType,
			                                     		ContentTypes.ContentTypes.ScheduleInterviewResultContentType
			                                     	};
			return contentTypes;
		}

		public override List<BaseEventReceiverObject> GetEventReceivers()
		{
			List<BaseEventReceiverObject> eventReceivers = new List<BaseEventReceiverObject>
			                                               	{
			                                               		new CandidatesListItemAddedEventReceiverObject()
			                                               	};
			return eventReceivers;
		}

		public override List<Workflow> GetWorkflows()
		{
			List<Workflow> workflows = new List<Workflow>
			                           	{
			                           		this.HrWorkflow
			                           	};
			return workflows;
		}

		/// <summary>
		/// Setups order of content type in "New" menu item
		/// </summary>
		/// <param name="list">Parent list of the folder</param>
		/// <param name="folder">Folder to setup</param>
		public void SetupContentTypeUniqueOrder(SPList list, SPFolder folder)
		{
			SPContentType[] contentTypes = list.ContentTypes.OfType<SPContentType>()
				.Where(contentType => !contentType.Id.IsChildOf(ContentTypes.ContentTypes.CandidateContentType.ID))
				.OrderBy(contentType => contentType.Name)
				.ToArray();

			folder.UniqueContentTypeOrder = contentTypes;
			folder.Update();

		}
	}
}
